package com.topscore.uke.mvp.presenters;

import android.graphics.Color;

import com.topscore.uke.entities.MemberConsumpInfo;
import com.topscore.uke.entities.TeamPerfomenceSummaryData;
import com.topscore.uke.entities.TeamPerformanceChartData;
import com.topscore.uke.entities.TeamPerformanceRankData;
import com.topscore.uke.entities.UserInfo;
import com.topscore.uke.mvp.contractors.GetTeamPerformanceRankContractor;
import com.topscore.uke.mvp.model.GetTeamPerformanceRankModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GetTeamPerformanceRequest;
import com.topscore.uke.network.request.GetTeamRankRequest;
import com.topscore.uke.utils.UserInfoManager;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;


public class GetTeamPerformanceRankPresenter implements GetTeamPerformanceRankContractor, GetTeamPerformanceRankContractor.Presenter {
    private Model mModel;
    private GetTeamPerformanceRequest mPerformanceRequest;
    private GetTeamRankRequest mRankRequest;
    private View mView;

    private int mCurrentPage = 1;
    private final int _PageSize = 15;

    Map<String, Object> mPageParams;

    public GetTeamPerformanceRankPresenter(View view) {
        this.mView = view;
        this.mPerformanceRequest = new GetTeamPerformanceRequest(UserInfoManager.getUserInfo().getLoginName(), getCurrentYear());
        this.mRankRequest = new GetTeamRankRequest(UserInfoManager.getUserInfo().getLoginName(), getCurrentYear());
        mPageParams = new HashMap<>();
        mPageParams.put("pageSize", _PageSize);
        mRankRequest.setPageParams(mPageParams);
    }


    @Override
    public void getPerformanceRank() {
        mView.onLoading("正在获取");
        mCurrentPage = 1;
        mPageParams.put("page", mCurrentPage);
        mRankRequest.setPageParams(mPageParams);
        mModel = new GetTeamPerformanceRankModel(new Model.CallBack() {
            @Override
            public void onGetSummarySuccess(BaseResponse<TeamPerfomenceSummaryData> response) {
                mView.getTotalAmountTv().setText(new DecimalFormat("0.00").format(response.getData().getSalesAmount()));
                mView.getTotalSalesCountTv().setText(String.valueOf(response.getData().getSalesCount()));
                mView.getLineColumnChartView().setComboLineColumnChartData(getChartData(response.getData().getSalesDetails()));
            }

            @Override
            public void onGetRankSuccess(BaseResponse<TeamPerformanceRankData> response) {
                mView.getRankAdapter().setNewData(response.getData().getDataList());
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mModel.getData(mPerformanceRequest, mRankRequest);
    }

    @Override
    public void getPerformanceRank(String year) {
        mView.onLoading("正在获取");
        mCurrentPage = 1;
        mPageParams.put("page", mCurrentPage);
        mRankRequest.setPageParams(mPageParams);
        mRankRequest.setYear(year);
        mPerformanceRequest.setYear(year);
        mModel = new GetTeamPerformanceRankModel(new Model.CallBack() {
            @Override
            public void onGetSummarySuccess(BaseResponse<TeamPerfomenceSummaryData> response) {
                mView.getTotalAmountTv().setText(new DecimalFormat("0.00").format(response.getData().getSalesAmount()));
                mView.getTotalSalesCountTv().setText(String.valueOf(response.getData().getSalesCount()));
                mView.getLineColumnChartView().setComboLineColumnChartData(getChartData(response.getData().getSalesDetails()));
            }

            @Override
            public void onGetRankSuccess(BaseResponse<TeamPerformanceRankData> response) {
                mView.getRankAdapter().setNewData(response.getData().getDataList());
                if (response.getData().getDataList().size()<_PageSize){
                    mView.getRankAdapter().loadMoreComplete();
                    mView.getRankAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mModel.getData(mPerformanceRequest, mRankRequest);
    }

    @Override
    public void getPerformanceRank(String year, String month) {
        mView.onLoading("正在获取");
        mCurrentPage = 1;
        mPageParams.put("page", mCurrentPage);
        mRankRequest.setPageParams(mPageParams);
        mRankRequest.setYear(year);
        mRankRequest.setMonth(month);
        mPerformanceRequest.setYear(year);
        mPerformanceRequest.setMonth(month);
        mModel = new GetTeamPerformanceRankModel(new Model.CallBack() {
            @Override
            public void onGetSummarySuccess(BaseResponse<TeamPerfomenceSummaryData> response) {
                mView.getTotalAmountTv().setText(new DecimalFormat("0.00").format(response.getData().getSalesAmount()));
                mView.getTotalSalesCountTv().setText(String.valueOf(response.getData().getSalesCount()));
                mView.getLineColumnChartView().setComboLineColumnChartData(getChartData(response.getData().getSalesDetails()));
            }

            @Override
            public void onGetRankSuccess(BaseResponse<TeamPerformanceRankData> response) {
                mView.getRankAdapter().setNewData(response.getData().getDataList());
                if (response.getData().getDataList().size() < _PageSize) {
                    mView.getRankAdapter().loadMoreComplete();
                    mView.getRankAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
        mModel.getData(mPerformanceRequest, mRankRequest);
    }

    @Override
    public void loadNextPage() {
        int page = mCurrentPage + 1;
        mPageParams.put("page", page);
        mRankRequest.setPageParams(mPageParams);
        mModel = new GetTeamPerformanceRankModel(new Model.CallBack() {
            @Override
            public void onGetSummarySuccess(BaseResponse<TeamPerfomenceSummaryData> response) {

            }

            @Override
            public void onGetRankSuccess(BaseResponse<TeamPerformanceRankData> response) {
                if (!response.getData().getDataList().isEmpty()) {
                    mCurrentPage = page;
                    mView.getRankAdapter().addData(response.getData().getDataList());
                    if (response.getData().getDataList().size() < _PageSize) {
                        mView.getRankAdapter().loadMoreComplete();
                        mView.getRankAdapter().loadMoreEnd();
                    }
                } else {
                    mView.getRankAdapter().loadMoreComplete();
                    mView.getRankAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.getRankAdapter().loadMoreFail();
            }

            @Override
            public void onComplete() {
                mView.getRankAdapter().loadMoreComplete();
            }
        });
        mModel.getRankData(mRankRequest);
    }

    @Override
    public void getRankListBySort(String sort) {

    }

    private ComboLineColumnChartData getChartData(List<TeamPerformanceChartData> salesDetails) {
        ComboLineColumnChartData chartData = new ComboLineColumnChartData(getColumnData(salesDetails), getLineData(salesDetails));
        List<AxisValue> axisXValues = new ArrayList<>();
        AxisValue axisXValue;
        for (int i = 0; i < salesDetails.size(); i++) {
            axisXValue = new AxisValue(i).setLabel(salesDetails.get(i).getDateIndex());
            axisXValues.add(axisXValue);
        }
        Axis axisX = new Axis(axisXValues);
        axisX.setHasTiltedLabels(false);
        chartData.setAxisXBottom(axisX);
        return chartData;
    }

    private ColumnChartData getColumnData(List<TeamPerformanceChartData> salesDetails) {
        List<Column> columns = new ArrayList<>();
        for (int i = 0; i < salesDetails.size(); i++) {
            SubcolumnValue subcolumnValue = new SubcolumnValue((float) salesDetails.get(i).getSalesAmount());
            subcolumnValue.setColor(Color.parseColor("#CE3B3E"));
            List<SubcolumnValue> subcolumnValues = new ArrayList<>();
            subcolumnValues.add(subcolumnValue);
            Column column = new Column(subcolumnValues);
            columns.add(column);
        }
        ColumnChartData columnChartData = new ColumnChartData(columns);
        return columnChartData;
    }

    private LineChartData getLineData(List<TeamPerformanceChartData> salesDetails) {
        List<Line> lines = new ArrayList<>();
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < salesDetails.size(); i++) {
            PointValue value = new PointValue(i, salesDetails.get(i).getSalesCount());
            pointValues.add(value);
        }
        Line line = new Line(pointValues);
        line.setPointRadius(3);
        line.setStrokeWidth(1);
        line.setColor(Color.parseColor("#f8b551"));
        lines.add(line);
        LineChartData lineChartData = new LineChartData(lines);
        return lineChartData;
    }

    public String getCurrentYear() {
        Date date = new Date();
        String year = new SimpleDateFormat("YYYY").format(date);
        return year;
    }

    public String getCurrentMonth() {
        Date date = new Date();
        String year = new SimpleDateFormat("MM").format(date);
        return year;
    }
}
